package com.gorkr.esjd.service;

import com.gorkr.esjd.common.ESCommon;
import com.gorkr.esjd.entity.Content;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author gorkr
 * @date 2022/04/08 18:04
 **/


@Service
public class ElasticsearchService {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * 实现关键词搜索
     *
     * @param keyword
     * @return
     */
    public List<Map<String, Object>> searchKeyword(String keyword, int from, int size) throws IOException {
        //搜索请求对象
        SearchRequest searchRequest = new SearchRequest(ESCommon.ES_INDEX);

        //构建搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchSourceBuilder.from(from);
        searchSourceBuilder.size(size);

        //查询条件
        // 这里为什么是term 而不是match 。 不需要设置ik分词器嘛
        TermQueryBuilder name = QueryBuilders.termQuery("name", keyword);
        searchSourceBuilder.query(name);

        searchRequest.source(searchSourceBuilder);

        //请求查询
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //结果封装
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            maps.add(sourceAsMap);
        }
        //查询结果返回
        return maps;
    }

//    /**
//     * 根据关键词进行解析数据添加到ES中供之后的检索
//     * @param keyword
//     * @return
//     * @throws IOException
//     */
//    // 可以在插入时使用ik分词器
//    public boolean parseAndInsertES(String keyword) throws IOException {
//        List<Content> contents;
//        //批量添加数据
//        BulkRequest bulkRequest = new BulkRequest(ESCommon.ES_INDEX);
//
//        //超时配置
//        bulkRequest.timeout(new TimeValue(60, TimeUnit.SECONDS));
//
//        //结果封装到请求中
//        for (int i = 0; i < contents.size(); i++) {
//            bulkRequest.add(new IndexRequest().source(giot
//                    , XContentType.JSON)
//            );
//        }
//
//        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
//
//        if (bulk.status() == RestStatus.OK) {
//            return true;
//        } else {
//            return false;
//        }
//    }

}
    